﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.Web.UI.WebControls;
using LTP.Accounts.Bus;
using System.Configuration;

namespace TCS.Web.uc
{
    public partial class AreaTree : System.Web.UI.UserControl
    {
        User currentUser;
        AccountsPrincipal user;
        private string _url;

        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        private bool _enabledclientscript=false;

        public bool EnabledClientScript
        {
            get { return _enabledclientscript; }
            set { _enabledclientscript = value; }
        }

        private bool _enabledparams;

        public bool Enabledparams
        {
            get { return _enabledparams; }
            set { _enabledparams = value; }
        }

        private string _targetframe;

        public string TargetFrame
        {
            get { return _targetframe; }
            set { _targetframe = value; }
        }
        
        public bool AutoPostBack
        {
            set { this.TreeView1.AutoPostBack = value; }
        }
        private bool _menuexpanded;

        public bool MenuExpanded
        {
            get { return _menuexpanded; }
            set { _menuexpanded = value; }
        }

        private string _selectText;

        public string SelectText
        {
            get { return  TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex).ID; }
            set { _selectText = value; }
        }
        private string _selectValue;

        public string SelectValue
        {
            get { return TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex).NodeData; }
            //set { _selectValue = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] == null)
                {
                    return;
                }
                user = new AccountsPrincipal(Context.User.Identity.Name);
                currentUser = (LTP.Accounts.Bus.User)Session["UserInfo"];

                DataSet ds = new TCS.BLL.Gsp_Administrative().GetListNew(0, "", "addid");

                BindTreeView(TargetFrame,ds.Tables[0]);
                    
            }
        }
        #region 绑定
        //邦定根节点
        public void BindTreeView(string TargetFrame, DataTable dt)
        {
            DataRow[] drs = dt.Select("ParentID= " + 0);//　选出所有子节点	
            if (user.IsInRole("超级管理员"))
            {
                drs = dt.Select("ParentID= " + 0);
            }
            else
            {
                drs = dt.Select("addid= " +currentUser.DepartmentID);
            }
            //菜单状态

            string zxq_url = Request.Url.ToString();
            if (zxq_url.Contains("AllList.aspx"))  //如果是信息浏览页面
            {
                drs = dt.Select("ParentID= " + 0);
            }
            bool menuExpand = MenuExpanded;

            TreeView1.Nodes.Clear(); // 清空树
            foreach (DataRow r in drs)
            {
                string nodeid = r["addid"].ToString();
                string text = r["addname"].ToString();
                string parentid = r["ParentID"].ToString();
                string location = "";
                string url = Url;
                if (Enabledparams)
                {
                    url += r["addid"].ToString();
                }
                string imageurl = "";
                int permissionid = 0;
                string framename = TargetFrame;

                //treeview set
                this.TreeView1.Font.Name = "宋体";
                this.TreeView1.Font.Size = FontUnit.Parse("9");

                //权限控制菜单		
               
                    Microsoft.Web.UI.WebControls.TreeNode rootnode = new Microsoft.Web.UI.WebControls.TreeNode();
                    rootnode.Text = "<span onclick=\"return Click_Node('" + nodeid + "','" + text + "');\">" + text + "</span>";
                    if (!EnabledClientScript)
                    {
                        rootnode.Text = text;
                    }
                    rootnode.NodeData = nodeid;
                    rootnode.NavigateUrl = url;
                    rootnode.Target = framename;
                    rootnode.Expanded = true;
                    rootnode.ID = text;
                    //rootnode.ImageUrl = imageurl;

                    TreeView1.Nodes.Add(rootnode);

                    int sonparentid = int.Parse(nodeid);// or =location
                    CreateNode(framename, sonparentid, rootnode, dt);
               


            }

        }

        //邦定任意节点
        public void CreateNode(string TargetFrame, int intparentid, Microsoft.Web.UI.WebControls.TreeNode parentnode, DataTable dt)
        {
            DataRow[] drs = dt.Select("ParentID= " + intparentid);//选出所有子节点			
            foreach (DataRow r in drs)
            {
                string nodeid = r["addid"].ToString();
                string text = r["addname"].ToString();
                string parentid = r["ParentID"].ToString();
                string location = "";
                string url = Url;
                if (Enabledparams)
                {
                    url += r["addid"].ToString();
                }
                string imageurl = "";
                int permissionid = 0;
                string framename = TargetFrame;

                //权限控制菜单
                

                    Microsoft.Web.UI.WebControls.TreeNode node = new Microsoft.Web.UI.WebControls.TreeNode();
                    node.Text = "<span onclick=\"return Click_Node('" + nodeid + "','" + text + "');\">" + text + "</span>";
                    if (!EnabledClientScript)
                    {
                        node.Text = text;
                    }
                    node.NodeData = nodeid;
                    node.NavigateUrl = url;
                    node.Target = TargetFrame;
                    node.ID = text;
                    //node.ImageUrl = imageurl;
                    //node.Expanded=true;
                    int sonparentid = int.Parse(nodeid);// or =location

                    if (parentnode == null)
                    {
                        TreeView1.Nodes.Clear();
                        parentnode = new Microsoft.Web.UI.WebControls.TreeNode();
                        TreeView1.Nodes.Add(parentnode);
                    }
                    parentnode.Nodes.Add(node);
                    CreateNode(framename, sonparentid, node, dt);
               

            }//endforeach		

        }
        #endregion

        protected void TreeView1_SelectedIndexChange(object sender, TreeViewSelectEventArgs e)
        {
           // this.SelectText = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex).Text;
           // this.SelectValue =TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex).NodeData;
           // Session["stext"] = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex).Text;
        }
       


    }
}